export default {
  namespaced: true,
  state: {
    token: ""
  },
  getters: {},
  mutations: {
    // payload =  {value:{}}
    setToken(state, payload) {
      state.token = payload.value;
    }
  },
  actions: {
    // async fetchToken({commit}) {
    async fetchToken({ commit }) {

      // 1 调用小程序的api 获取当前用户信息
      const [err1, res1] = await uni.getUserProfile({
        desc: "用来给用户提供更好的服务",
      });
      //  获取接口要的用户信息

      const { encryptedData, rawData, iv, signature } = res1;
      // console.log(encryptedData, rawData, iv, signature);

      // 2 执行微信的登录 login 获取临时登录凭据
      const [err2, res2] = await uni.login();
      const { code } = res2;

      // 3 向后端来获取 token
      const res3 = await uni.$u.http.post("/users/wxlogin", {
        encryptedData,
        rawData,
        iv,
        signature,
        code,
      });
      const { token } = res3;
      commit("setToken", { value: token });
    }
  }
}